package com.bird.main;

import com.bird.util.Constant;
import com.bird.util.GameUtil;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Barrier {
    public static final int TYPE_TOP_NORMAL = 0;
    public static final int TYPE_BOT_NORMAL = 2;
    public static final int TYPE_MID_NORMAL = 4;
    public static final int TYPE_MOVE_NORMAL = 6;
    public static final int BARRIER_WIDTH;
    public static final int BARRIER_HEIGHT;
    public static final int BARRIER_HEAD_WIDTH;
    public static final int BARRIER_HEAD_HEIGHT;
    private static BufferedImage[] images;

    //初始化参数
    static {
        final int COUNT = Constant.COLUMN_IMG_PATH.length;
        //类一旦加载将三个图片初始化
        images = new BufferedImage[COUNT];
        for (int i = 0; i < COUNT; i++) {
            images[i] = GameUtil.loadBufferedImage(Constant.COLUMN_IMG_PATH[i]);
        }
        // 确保图片加载成功后再初始化这些常量
        // TODO:确保不会出现空指针错误
        BARRIER_WIDTH = images[0] != null ? images[0].getWidth() : 0;
        BARRIER_HEIGHT = images[0] != null ? images[0].getHeight() : 0;
        BARRIER_HEAD_WIDTH = images[1] != null ? images[1].getWidth() : 0;
        BARRIER_HEAD_HEIGHT = images[1] != null ? images[1].getHeight() : 0;
    }

    private Rectangle rectangle;
    private int speed = 10;
    private int x, y, height, type;     //width不再设置没用！
    private boolean isVisible;
    private boolean mob = true;

    public Barrier() {
        rectangle = new Rectangle();
    }

    public Rectangle getRectangle() {
        return rectangle;
    }

    public void drawRect(Graphics g) {
        g.setColor(Color.RED);
        //绘制矩形的时候,x+speed确保视觉上障碍物和矩形一致
        //如果矩形的坐标不同步,将无法检测碰撞
        rectangle.width = BARRIER_WIDTH;
        rectangle.height = height;
        rectangle.x = x;
        rectangle.y = y;

        //TODO:障碍物的矩形依旧存在,只是不再绘制出来
        //g.drawRect(x + speed, y, BARRIER_WIDTH, height);
    }

    public void draw(Graphics g) {
        switch (type) {
            case TYPE_TOP_NORMAL:
                drawTopNormal(g);
                break;
            case TYPE_BOT_NORMAL:
                drawNormalTop(g);
                break;
            case TYPE_MID_NORMAL:
                drawNormalMid(g);
                break;
            case TYPE_MOVE_NORMAL:
                drawNormalMove(g);
                break;
        }
        drawRect(g);
    }

    public boolean getIsVisible() {
        return isVisible;
    }

    public void setVisible(boolean visible) {
        isVisible = visible;
    }

    private void drawTopNormal(Graphics g) {
        int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT;
        //循环绘制障碍物
        for (int i = 0; i <= count; i++) {
            g.drawImage(images[0], x, y + i * BARRIER_HEIGHT, null);
        }
        g.drawImage(images[2], x - (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, height - BARRIER_HEAD_HEIGHT, null);
        x -= speed;
        if (x < -50) {
            this.isVisible = false;
        }


    }


    private void drawNormalTop(Graphics g) {
        int count = height / BARRIER_HEIGHT;
        //循环绘制障碍物
        for (int i = 0; i <= count; i++) {
            g.drawImage(images[0], x, Constant.FRAM.getHeight() - i * BARRIER_HEIGHT, null);
        }
        g.drawImage(images[1], x - (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, Constant.FRAM.getHeight() - height, null);
        x -= speed;
        if (x < -50) {
            this.isVisible = false;
        }

    }

    private void drawNormalMid(Graphics g) {
        int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT;
        g.drawImage(images[1], x, y, null);
        //循环绘制障碍物
        for (int i = 0; i < count; i++) {
            g.drawImage(images[0], x, y + BARRIER_HEAD_HEIGHT + i * BARRIER_HEIGHT, null);
        }
        g.drawImage(images[2], x, y + height - BARRIER_HEAD_HEIGHT, null);
        x -= speed;
        if (x < -50) {
            this.isVisible = false;
        }
    }

    private void drawNormalMove(Graphics g) {
        int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT;
        g.drawImage(images[1], x, y, null);
        //循环绘制障碍物
        for (int i = 0; i < count; i++) {
            g.drawImage(images[0], x, y + BARRIER_HEAD_HEIGHT + i * BARRIER_HEIGHT, null);
        }
        g.drawImage(images[2], x, y + height - BARRIER_HEAD_HEIGHT, null);
        x -= speed;
        if (x < -50) {
            this.isVisible = false;
        }
        if (mob) {
            y += 5;
            if (y >= 250) {
                mob = false;
            }
        } else {
            y -= 5;
            if (y <= 100) {
                mob = true;
            }
        }
    }

    //这个障碍物是否已经进入屏幕内?
    public boolean isIn() {
        return x < 100;
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}
